Strings¶

In [ ]:
'this is a string'

🖌 Building Strings¶

In [ ]:
'fire' + 'place'
In [ ]:
'yo' * 2
In [ ]:
'nan ' * 16 + 'batman!'
batman!

👩🏼‍🎨 Put it in a Banner¶

Write a function that wraps text in a banner of dashes.

For example,

'This statement would look better in a banner.'

becomes

'---------------------------------------------\nThis statement would look better in a banner.\n---------------------------------------------'

which prints as:

---------------------------------------------
This statement would look better in a banner.
---------------------------------------------
In [ ]:
def banner(text):
    """Wrap the text in banner"""
    num_dash = len(text)
    return '-' * num_dash + '\n' + text + '\n' + '-' * num_dash
In [ ]:
def banner(text):
    """Wrap the text in banner"""
    size = len(text)
    return '-' * size + '\n' + text + '\n' + '-' * size
In [ ]:
result = banner('Go cougars!')
print(result)
In [ ]:
result = banner('Cosmo is the best mascot in the world. Go cougars!')
print(result)

👨🏿‍🎨 Customize the Banner¶

Modify the banner function so you can specify what character to use in the banner.

In [ ]:
def banner(text, banner_char):
    """Wrap the text in banner"""
    num_dash = len(text)
    return banner_char * num_dash + '\n' + text + '\n' + banner_char * num_dash
In [ ]:
def banner(text, banner):
    """Wrap the text in banner"""
    size = len(text)
    return banner * size + '\n' + text + '\n' + banner * size
In [ ]:
print(banner('This is epic', '*'))
In [ ]:
print(banner('I got a job as a CS TA, and now I bring home bacon.', '$'))
In [ ]:
print(banner('It snowed on the first day of summer!', '\u2603'))

🖌 String Iteration¶

In [ ]:
for letter in 'this is a string':
    print(letter)

🎨 String is... Methods¶

In [ ]:
'a'.isalpha(), '8'.isalpha()
In [ ]:
'a'.isdigit(), '8'.isdigit()
In [ ]:
'a'.isalnum(), '8'.isalnum()
In [ ]:
'a'.isspace(), '8'.isspace(), ' '.isspace()
In [ ]:
'a'.isalnum(), '8'.isalnum(), ' '.isalnum()
In [ ]:
'a'.isspace(), '8'.isspace(), ' '.isspace()
In [ ]:
characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*() \t\n'
In [ ]:
# isalpha
keepers = []
for character in characters:
    if character.isalpha():
        keepers.append(character)
print(keepers)
In [ ]:
# isdigit
keepers = []
for character in characters:
    if character.isdigit():
        keepers.append(character)
print(keepers)
In [ ]:
# isspace
keepers = []
for character in characters:
    if character.isspace():
        keepers.append(character)
print(keepers)

👨🏾‍🎨 Numbers?¶

Write a function that replaces every number in a string with ?

In [ ]:
def no_numbers(text):
    result = ''
    for letter in text:
        if letter.isdigit():
            result = result + '?'
        else:
            result = result + letter
    return result
In [ ]:
def no_numbers(text):
    result = ''
    for char in text:
        if char.isdigit():
            result = result + '?'
        else:
            result = result + char
    return result
In [ ]:
no_numbers('There were 7 people.')
In [ ]:
no_numbers('15 out of 25 have more than 17.3% contamination.')
In [ ]:
no_numbers('2 + 2 = 5, for large values of 2.')

🖌 +=¶

In [ ]:
message = 'Hello'
message = message + ' world!'
message
In [ ]:
message = 'Hello'
message += ' world!'
message

👩🏻‍🎨 No Spaces¶

Write a function that replaces all space characters with dashes.

In [ ]:
def no_spaces(text):
    result = ''
    for letter in text:
        if letter.isspace():
            result += '-'
        else:
            result += letter
    return result
In [ ]:
def no_spaces(text):
    result = ''
    for c in text:
        if c.isspace():
            c = '-'
        result += c
    return result
In [ ]:
print(no_spaces('BYU is the place to be.'))
In [ ]:
message = """This is a long,
multiline string.
It has multiple lines.
That is what "multiline" means. :)"""

print(message)
print()
print(no_spaces(message))
In [ ]:
print(no_spaces('Goodbye spaces \t tabs \n and newlines'))

🎨 Capitalization¶

In [ ]:
'A'.islower(), 'A'.isupper()
In [ ]:
'a'.islower(), 'a'.isupper()
In [ ]:
'a'.upper(), 'a'.lower()
In [ ]:
'A'.upper(), 'A'.lower()

🎨 in¶

In [ ]:
'BYU' in 'I am a student at BYU.'
In [ ]:
'BYU' in 'This room is full of monkeys!'
In [ ]:
vowels = 'aeiou'
vowels += vowels.upper()
print(vowels)

found = ''
for letter in 'The Aeneid is ancient Greek literature.':
    if letter in vowels:
        found += letter
print(found)
In [ ]:
vowels = 'aeiou'
print(vowels)

found = ''
for letter in 'The Aeneid is ancient Greek literature.':
    if letter.lower() in vowels:
        found = found + letter
print(found)

🧑🏽‍🎨 BYU Enthusiasts¶

Capitalize every letter in an input string that is part of BYU.

In [ ]:
def byu(text):
    """Capitalize every letter of `text` that is part of 'BYU'"""
    result = ''
    for letter in text:
        if letter in 'byu':
            result += letter.upper()
        else:
            result += letter
    return result
In [ ]:
def byu(text):
    """Capitalize every letter of `text` that is part of 'BYU'"""
    result = ''
    for c in text:
        if c in 'byu':
            result += c.upper()
        else:
            result += c
    return result
In [ ]:
print(byu('write "byu" in all-caps'))
In [ ]:
print(byu('Any student, boy or girl, young or old, can be a yodeler.'))

🧑🏻‍🎨 Sum of Digits¶

Add up all the digits found in a string.

In [ ]:
def add_digits(text):
    """Add all the digits found in a `text`. 
    
    >>> add_digits('123foo')
    6
    """
    total = 0
    for letter in text:
        if letter.isdigit():
            total += int(letter)
    return total
In [ ]:
def add_digits(text):
    """Add all the digits found in a `text`. 
    
    >>> add_digits('123foo')
    6
    """
    total = 0
    for c in text:
        if c.isdigit():
            total += int(c)
    return total
In [ ]:
add_digits('123foo')
In [ ]:
add_digits('10 students ate 6 oranges and 42 students ate 7 pears.')

Key Ideas¶

  • String iteration
  • 'foo' + 'bar', 'BYU! ' * 5
  • .isalpha(), .isdigit(), .isalnum(), .isspace(), .isupper(), .islower()
  • .upper(), '.lower()
  • +=
  • in, i.e. 'BYU' in 'my favorite school is BYU'